﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Linq;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1.SensorTable
{
    public partial class FormMonitorDev : Form
    {
        private bool change = false;

        public FormMonitorDev()
        {
            InitializeComponent();
        }

        private void tb_monitor_deviceBindingNavigatorSaveItem_Click(object sender, EventArgs e)
        {
            try
            {
                tb_monitor_deviceDataGridView.EndEdit();
                Monitor_DB.instance.db.SubmitChanges();
                change = false;

                MessageBox.Show("保存成功！", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message, "错误！", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Monitor_DB.instance.Refresh_DB();
            }
        }

        private void FormMonitorDev_Load(object sender, EventArgs e)
        {
            try
            {
                Monitor_DB.instance.db.Refresh(RefreshMode.KeepChanges, Monitor_DB.instance.db.tb_monitor_device);
                this.tb_monitor_deviceBindingSource.DataSource = Monitor_DB.instance.db.tb_monitor_device;

                
                // 手机号ID
                var query =
                    from mon in Monitor_DB.instance.db.tb_mobileNum
                    select mon;
                foreach (var item in query)
                {
                    Mobile_num_Id.Items.Add(item.ID);
                }
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message, "错误！", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void mobile_num_IdTextBox_TextChanged(object sender, EventArgs e)
        {
            // 手机号
            var numQuery =
                from num in Monitor_DB.instance.db.tb_mobileNum
                where num.ID == Convert.ToInt32(this.mobile_num_IdTextBox.Text)
                select num;
            foreach (var item in numQuery)
            {
                this.mobile_num_IdTextBox.Text = item.Mobile_num;
                break;
            }
        }

        private void splitContainer1_Panel2_Paint(object sender, PaintEventArgs e)
        {

        }

        private void tb_monitor_deviceDataGridView_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Control == true && e.KeyCode == Keys.S)
            {
                tb_monitor_deviceBindingNavigatorSaveItem_Click(sender, e);
            }
        }

        private void nameTextBox_TextChanged(object sender, EventArgs e)
        {

        }

        private void FormMonitorDev_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (change)
            {
                DialogResult r = MessageBox.Show("保存修改？", "关闭", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
                if (r == DialogResult.Yes)
                {
                    tb_monitor_deviceBindingNavigatorSaveItem_Click(sender, e);
                }
                else if (r == DialogResult.Cancel)
                {
                    e.Cancel = true;

                }
            }
        }

        private void tb_monitor_deviceDataGridView_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
        {
            change = true;
        }

        private void inUseCheckBox_Click(object sender, EventArgs e)
        {
            change = true;
        }

        private void alarmTextBox_Click(object sender, EventArgs e)
        {
            change = true;
        }

        private void nameTextBox_Click(object sender, EventArgs e)
        {
            change = true;
        }

        private void tb_monitor_deviceDataGridView_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            //int len = tb_monitor_deviceDataGridView.SelectedRows[0].Cells[2].Value.ToString().Length;
            //if (len != 10 && len != 0)
            //{
            //    MessageBox.Show("监控箱设备号必须是10位！", "错误！", MessageBoxButtons.OK, MessageBoxIcon.Error);
            //}
        }


    
    }
}
